<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use app\common\model\User;
use fast\Random;
use think\Session;
use think\Validate;


class Info extends Backend
{

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\UserInfo;

    }

    public function index(){

        if ($this->request->isAjax())
        {
            $model = model('UserInfo');
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $model
                ->where($where)
                ->where('uid', $this->auth->aid)
                ->order($sort, $order)
                ->count();

            $list = $model
                ->where($where)
                ->where('uid', $this->auth->aid)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加联系人
     * @return string
     */
    public function add(){

        if($this->request->isPost()){
            $params = $this->request->post('row/a');
            if($params){
                $params['uid'] = $this->auth->aid;
                $params['uuid'] = $this->auth->id;
                $userInfo = model('UserInfo');
                $result = $userInfo->validate('UserInfo.add')->save($params);
                if($result === false){
                    $this->error($userInfo->getError());
                }
                $this->success();
            }
            $this->error('提交失败');
        }
        return $this->view->fetch();
    }

    /**
     * 修改密码
     */
    public function update()
    {
        if ($this->request->isPost())
        {
            $params = $this->request->post("row/a");

            $rule = [
                'oldpassword'  => 'require',
                'password'  => 'require|length:6,30',
                'cofpassword'  => 'require|length:6,30',
            ];
            $msg = [
                'oldpassword.require' => 'oldPassword can not be empty',
                'password.require' => 'Password can not be empty',
                'password.length'  => 'Password must be 6 to 30 characters',
                'cofpassword.require' => 'cofPassword can not be empty',
                'cofpassword.length'  => 'cofPassword must be 6 to 30 characters',
            ];
            $data = [
                'oldpassword'  => $params['oldpassword'],
                'password'  => $params['password'],
                'cofpassword'  => $params['cofpassword']
            ];
            $validate = new Validate($rule,$msg);
            $result = $validate->check($data);
            if (!$result) {
                $this->error(__($validate->getError()));
            }

            if(isset($params['oldpassword'])){
                $user = User::get(['login_name' => $params['username']]);
                if($user->password != md5(md5($params['oldpassword']) . $user->salt)){
                    $this->error(__('Password is incorrect'));
                }
            }
            if($params['password'] != $params['cofpassword']){
                $this->error(__('Passwords do not match'));
            }

            if (isset($params['password']))
            {
                $cData['salt'] = Random::alnum();
                $cData['password'] = md5(md5($params['password']) . $cData['salt']);

                $user = User::get(['aid' => $this->auth->aid]);
                $user->save($cData);
                Session::set("admin", $user->toArray());
                $this->success(__('Reset password successful'));
            }
            $this->error();

        }
        return;
    }
    

}
